/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.boundary;

import it.polimi.meteocal.control.EventManager;
import it.polimi.meteocal.control.InvitationManager;
import it.polimi.meteocal.control.MessageManager;
import it.polimi.meteocal.entity.Message;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named("messageBean")
@ViewScoped
public class MessageBean implements Serializable {

    @EJB
    private MessageManager mm;
    @EJB
    private InvitationManager im;
    @EJB
    private EventManager em;

    private ArrayList<Message> messages;

    private Message selectedMessage;

    public Message getSelectedMessage() {
        return selectedMessage;
    }

    public void setSelectedMessage(Message selectedMessage) {
        this.selectedMessage = selectedMessage;
    }

    @PostConstruct
    public void init() {
        messages = mm.getMessage();

    }

    public ArrayList<Message> getMessages() {
        return messages;
    }

    public void setMessages(ArrayList<Message> messages) {
        this.messages = messages;
    }

    public boolean possibleAction(int i) {
        boolean b = false;
        if (selectedMessage != null) {
            if (selectedMessage.getEvent().getDeleted()) {
                return true;
            }

            switch (selectedMessage.getType()) {
                case 0:
                    switch (i) {
                        case 0:
                            return true;
                        case 1:
                            return true;
                        case 2:
                            return true;
                        case 3:
                            return false;
                    }
                    break;
                    case 3:
                    switch (i) {
                        case 0:
                            return true;
                        case 1:
                            return true;
                        case 2:
                            return false;
                        case 3:
                            return false;
                    }
                    break;
                        case 4:
                    switch (i) {
                        case 0:
                            return true;
                        case 1:
                            return true;
                        case 2:
                            return false;
                        case 3:
                            return false;
                    }
                    break;
                case 2:
                    if (im.alreadyAnswered(selectedMessage.getEvent())) {
                        switch (i) {
                            case 0:
                                return true;
                            case 1:
                                return true;
                            case 2:
                                return false;
                            case 3:
                                return false;
                        }
                    } else {
                        switch (i) {
                            case 0:
                                return false;
                            case 1:
                                return false;
                            case 2:
                                return false;
                            case 3:
                                return true;
                        }
                    }
                    break;
                case 1:
                    switch (i) {
                        case 0:
                            return true;
                        case 1:
                            return true;
                        case 2:
                            return false;
                        case 3:
                            return false;
                    }
                    break;
            }
        }

        return (b);
    }

    public void deleteSelected() {
        messages.remove(selectedMessage);
        mm.remove(selectedMessage);
    }

    public void accept() {
        im.accept(selectedMessage.getEvent());
    }

    public void declined() {
        im.decline(selectedMessage.getEvent());
    }

    public String gotoevent() {
        return ("eventdetail?faces-redirect=true&idevent=" + selectedMessage.getEvent().getEventID());
    }

    public String getStyle(Message m) {
        switch (m.getType()) {
            case 0:
                return "messagehighDeletedEvent";
            case 3:
                return "messagehighNormal";
            case 4:
                return "messagehighNormal";
            case 2:
                return "messagehighAcceptinvite";
            case 1:
                return "messagehighGotoEvent";
        }
        return ("");
    }

    public String getImage() {
        if (selectedMessage != null) {
            switch (selectedMessage.getType()) {
                case 0:
                    return "service_message.png";
                case 3:
                    return "service_message.png";
                case 4:
                    return "service_message.png";
                case 2:
                    return "invitation_message.png";
                case 1:
                    return "event_change_message.png";
            }
        }
        return ("");
    }

    public String intestazione(Message message) {
        switch (message.getType()) {
                case 0:
                    return "New message from: " + message.getEvent().getAuthor().getEmail();
                case 3:
                    return "Wheather Service";
                case 4:
                    return "Wheather Service";
                case 2:
                    return "New message from: " + message.getEvent().getAuthor().getEmail();
                case 1:
                    return "New message from: " + message.getEvent().getAuthor().getEmail();
            }
        return ("");
    }

    public String setPending() {
        mm.setPending();
        return "";
    }

    public String getPubblicationDate() {
        if (selectedMessage != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy / hh/mm");
            return (dateFormat.format(selectedMessage.getPubblication().getTime()));
        }
        return ("");
    }

    public MessageManager getMm() {
        return mm;
    }

    public void setMm(MessageManager mm) {
        this.mm = mm;
    }

    public InvitationManager getIm() {
        return im;
    }

    public void setIm(InvitationManager im) {
        this.im = im;
    }

    public EventManager getEm() {
        return em;
    }

    public void setEm(EventManager em) {
        this.em = em;
    }
    
}
